package feng.demo.controller.mer;

import cn.dev33.satoken.stp.StpUtil;
import feng.demo.dao.LargefileDao;
import feng.demo.entity.*;
import feng.demo.service.BreedService;
import feng.demo.service.CommodityService;
import feng.demo.service.TypeService;
import feng.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

@RestController
@RequestMapping("/mer/com")
public class MerComController {

    @Autowired
    CommodityService commodityService;

    @Autowired
    LargefileDao largefileDao;

    @Autowired
    UserService userService;

    @Autowired
    TypeService typeService;
    @Autowired
    BreedService breedService;
    @GetMapping("/findAll/{p}/{l}")
    public String findAll(@PathVariable("p")Integer p, @PathVariable("l")Integer l){
        Object phone = StpUtil.getLoginId();
        User user = userService.findByPhone(String.valueOf(phone));
        Map<String,Object> map=new HashMap<>();
        List<Commodity> list = commodityService.findByUidLimit(user.getUid(),p,l);
        int count = commodityService.CountByUid(user.getUid());
        map.put("com",list);
        map.put("count",count);
        return new XJson(200,"",map).toString();
    }
    @GetMapping("/findByAddAll")
    public String findfindByAddAll(){
//        添加全部下拉框
        Map<String,Object> map=new HashMap<>();
        List<Type> types = typeService.findAll();
        map.put("type",types);
        return new XJson(200,"",map).toString();
    }
    @GetMapping("/findByAddAllId/{id}")
    public String findByAddAllId(@PathVariable("id")Integer comid){
        Map<String,Object> map=new HashMap<>();
        Commodity com = commodityService.findById(comid);
        List<Type> types = typeService.findAll();
        map.put("com",com);
        map.put("type",types);
        return new XJson(200,"",map).toString();
    }

    @PostMapping("/add")
    public String add(Commodity commodity, MultipartFile img){
        Object phone = StpUtil.getLoginId();
        User user = userService.findByPhone(String.valueOf(phone));
        commodity.setUser(user);
        if (img==null){
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(commodity.getTmake());

            // 将日期加二天
            calendar.add(Calendar.DAY_OF_YEAR, 1);

            // 获取加一天后的 Date 对象
            Date newDate = calendar.getTime();
            commodity.setTmake(newDate);
            commodityService.add(commodity);

        }else {
            Largefile largefile = new Largefile();
            try {
                largefile.setFilename(img.getOriginalFilename());
                byte[] bytes=img.getBytes();
                largefile.setContent(bytes);
            }catch (Exception e){

            }
            Integer add = largefileDao.add(largefile);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(commodity.getTmake());

            // 将日期加二天
            calendar.add(Calendar.DAY_OF_YEAR, 1);

            // 获取加一天后的 Date 对象
            Date newDate = calendar.getTime();
            commodity.setTmake(newDate);
            commodity.setPic(largefile.getId());
            commodityService.add(commodity);
        }
        return new XJson(200,"添加成功","").toString();
    }

    @PostMapping("/update")
    public String update(Commodity commodity,MultipartFile img){
        Object phone = StpUtil.getLoginId();
        User user = userService.findByPhone(String.valueOf(phone));
        commodity.setUser(user);
        if (img==null){
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(commodity.getTmake());

            // 将日期加二天
            calendar.add(Calendar.DAY_OF_YEAR, 1);

            // 获取加一天后的 Date 对象
            Date newDate = calendar.getTime();
            commodity.setTmake(newDate);
            commodityService.update(commodity);
        }else {
            largefileDao.delete(commodity.getPic());
            Largefile largefile = new Largefile();
            try {
                largefile.setFilename(img.getOriginalFilename());
                byte[] bytes=img.getBytes();
                largefile.setContent(bytes);
            }catch (Exception e){

            }
            Integer add = largefileDao.add(largefile);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(commodity.getTmake());

            // 将日期加二天
            calendar.add(Calendar.DAY_OF_YEAR, 1);

            // 获取加一天后的 Date 对象
            Date newDate = calendar.getTime();
            commodity.setTmake(newDate);
            commodity.setPic(largefile.getId());
            commodityService.update(commodity);
        }
        return new XJson(200,"修改成功","").toString();
    }

    @GetMapping("/del/{id}/{p}/{l}")
    public String del(@PathVariable("id")Integer comid,@PathVariable("p")Integer p,@PathVariable("l")Integer l){
        Object phone = StpUtil.getLoginId();
        User user = userService.findByPhone(String.valueOf(phone));
        int del = commodityService.del(comid);
        Map<String,Object> maps=new HashMap<>();
        List<Commodity> list = commodityService.findByUidLimit(user.getUid(),p,l);
        int count = commodityService.CountByUid(user.getUid());
        maps.put("com",list);
        maps.put("count",count);
        return new XJson(200,"删除成功",maps).toString();
    }
}
